﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Xml;
using System.Configuration;
using System.Collections.Specialized;
using Redis.Utils;
using redis测试;
namespace redis测试
{
    [Serializable]//实例可以被序列化
    public class UserInfo
    {
        public long Id { set; get; }
        public string UserName { get; set; }
        public int Age { get; set; }
    }

    class Program
    {


        /// <summary>
        /// 测试redis
        /// </summary>
        //static void TestRedis()
        //{
        //    //設置數據
        //    List<string> storeMembers = 
        // new List<string>() { "one", "two", "three" };
        //    storeMembers.ForEach(x => Redis.AddItemToList("additemtolist", x));

        //    //得到數據
        //    var members = Redis.GetAllItemsFromList("additemtolist");
        //    members.ForEach(s => Console.WriteLine("<br/>additemtolist :" + s));

        //    // 获取指定索引位置数据 
        //    var item = Redis.GetItemFromList("addarrangetolist", 2);
        //    Console.WriteLine(item);

        //    //移除數據
        //    var list = Redis.Lists["addarrangetolist"];
        //    list.Clear();//清空
        //    list.Remove("two");//移除指定键值
        //    //  list.RemoveAt(2);//移除指定索引位置数据

        //    //存储对象（JSON序列化方法）它比object序列化方法效率高
        //    Redis.Set<UserInfo>("userinfo", new UserInfo() { UserName = "李四", Age = 45 });
        //    UserInfo userinfo = Redis.Get<UserInfo>("userinfo");
        //    Console.WriteLine("name=" + userinfo.UserName + "age=" + userinfo.Age);

        //    //存储值类型数据
        //    Redis.Set<int>("my_age", 12);//或Redis.Set("my_age", 12);
        //    int age = Redis.Get<int>("my_age");
        //    Console.WriteLine("age=" + age);

        //    //object序列化方式存储
        //    var ser = new ObjectSerializer();    //位于namespace ServiceStack.Redis.Support;
        //    bool result = Redis.Set<byte[]>("userinfo2", ser.Serialize(new UserInfo() { UserName = "张三", Age = 12 }));
        //    UserInfo userinfo2 = ser.Deserialize(Redis.Get<byte[]>("userinfo2")) as UserInfo;
        //    Console.WriteLine("name=" + userinfo2.UserName + "age=" + userinfo2.Age);


        //    //也支持列表
        //    List<UserInfo> userinfoList = new List<UserInfo> {
        //     new UserInfo{UserName="zzl",Age=1,Id=1},
        //     new UserInfo{UserName="zhz",Age=3,Id=2},
        //     };
        //    Redis.Set<byte[]>("userinfolist_serialize", ser.Serialize(userinfoList));
        //    List<UserInfo> userList = ser.Deserialize(Redis.Get<byte[]>("userinfolist_serialize")) as List<UserInfo>;
        //    userList.ForEach(i =>
        //    {
        //        Console.WriteLine("name=" + i.UserName + "age=" + i.Age);
        //    });

        //    //读取命令行中的key值
        //    string somevalue = Redis.Get<string>("mykey");
        //    Console.WriteLine("somevalue=" + somevalue);

        //    Redis.Utils.RedisClient rClient = new global::Redis.Utils.RedisClient();
        //    rClient.Set("name", "zzl");
        //    Console.WriteLine("name=" + rClient.Get("name"));
        //    Console.WriteLine("chat=" + rClient.Get("Chat"));
        //}

        /// <summary>
        /// 测试数据结构
        /// </summary>
        static void TestDataStructure()
        {
            #region Hash集合
            Person zhang = new Person("张", "三");
            Person li = new Person("李", "四");
            Person wang = new Person("王", "五");
            Hashtable hash = new Hashtable();
            hash.Add("zhang", zhang);
            hash.Add("li", li);
            hash.Add("wang", wang);
            foreach (DictionaryEntry p in hash)
            {
                Person pp = p.Value as Person;
                Console.Write(pp.FirstName + pp.LastName + "\n\r");
            }
            Console.Write("----------------\n\r");
            foreach (Person p in hash.Values)  //Values为Person的集合
            {
                Console.Write(p.FirstName + p.LastName + "\n\r");
            }

            #endregion
            #region SortList可排序的集合
            SortedList list = new SortedList();
            list.Add("a", zhang);
            list.Add("b", li);
            list.Add("c", wang);
            int index = list.IndexOfKey("d");
            Console.WriteLine(index);
            index = list.IndexOfValue(zhang);
            Console.WriteLine(index);
            Person p1 = (Person)list.GetByIndex(index);
            Person pp2 = (Person)list["d"];
            #endregion
            #region 队列结构，先进先出
            Queue qu = new Queue();
            qu.Enqueue("hello ");
            qu.Enqueue("world ");
            qu.Enqueue("!");
            int nums = qu.Count;
            for (int i = 0; i < nums; i++)
                Console.WriteLine(qu.Dequeue().ToString());
            Console.WriteLine("------------------集合被pop后，为空了就");
            foreach (string str in qu)
                Console.WriteLine(str);
            #endregion
            #region 堆栈结构，后进先出
            Stack sk = new Stack();
            sk.Push("hello ");
            sk.Push("world ");
            sk.Push("!");
            int nums1 = sk.Count;
            for (int i = 0; i < nums; i++) //典型的后进先出结构
            {
                Console.WriteLine(sk.Pop().ToString());
            }
            #endregion

        }
        static RedisClient rd = new RedisClient();
        static RedisClient_SortedSet rds = new RedisClient_SortedSet();
        static RedisClient_Hash rdh = new RedisClient_Hash();
       
        static void Main(string[] args)
        {
            Person p1 = new Person("zhang", "zhanling");
            Person p2 = new Person(2, "zhang", "hangzheng");
            Person p3 = new Person(3, "zhang", "qiaoli");
            Person p4 = new Person(4, "zhao", "rose");
            #region String
            rd.Set("str", "hello world!");
            rd.Set("per", p1);
            Console.WriteLine(rd.Get("str"));
            Console.WriteLine((rd.Get("per") as Person).LastName);
            #endregion

            #region Hash
            #endregion

            #region List
            rd.LeftPush("pp", p1);
            rd.LeftPush("pp", p2);
            rd.LeftPush("pp", p3);
            Console.WriteLine((rd.LeftPop("pp") as Person).LastName);
            #endregion

            #region Set
            rd.AddToSet("ps", p1);
            rd.AddToSet("ps", p2);
            rd.AddToSet("ps", p3);
            rd.GetMembersOfSet("ps").Cast<Person>().ToList().ForEach(i => Console.WriteLine(i.UserID + i.LastName));
            #endregion

            #region Sorted Set
            rds.Add("pss", 2, p1);
            rds.Add("pss", 1, p2);
            rds.Add("pss", 3, p3);
            rds.GetList("pss").Cast<Person>().ToList().ForEach(i => Console.WriteLine(i.UserID + i.LastName));
            #endregion


            #region 从数据库中建立一个redis缓存对象

            try
            {
               // rds.SelectDB(6);

               // new Data.Sql.Res_ItemRepository().GetDetailModel().Take(10000).ToList().ForEach(i => //所有属性,通过查询scorce的范围来锁定查询范围
               // {
               //     rds.Add("Res_Item_VID", i.VID, i.ID);
               //     //rds.BackgroundSave(); //只序列化最后一个
               // });

               // new Data.Sql.Res_ItemRepository().GetDetailModel().Take(10000).ToList().ForEach(i =>//所有分类
               //{
               //    rds.Add("Res_Item_CID", i.CategoryID, i.ID);
               //    //rds.BackgroundSave(); //只序列化最后一个
               //});


               // new Data.Sql.Res_ItemRepository().GetDetailModel().Take(10000).ToList().ForEach(i =>
               // {
               //     rds.Add("Res_Item", i.ID, i);
               //     //rds.BackgroundSave(); //只序列化最后一个
               // });


                //  rd.BackgroundSave();//序列化到磁盘




            }
            catch (Exception)
            {

                throw;
            }
            #endregion

            Console.WriteLine("数据插入完成");
            #region 从redis文件中读数据库内容
            //   new Data.Redis.PersonRepository().GetModel().ToList().ForEach(i => Console.WriteLine(i.ID + i.Name + i.PackageTime));
            #endregion
            // TestDataStructure();v

            Console.ReadKey();
        }
    }

    [Serializable]
    public class Person
    {
        public Person()
        { }
        public Person(string firstName, string lastName)
            : this(1, firstName, lastName)
        { }
        public Person(int userID, string firstName, string lastName)
        {
            this.UserID = userID;
            this.FirstName = firstName;
            this.LastName = lastName;
        }

        public int UserID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

}




